<?php


namespace app\api\controller\worker;

/**
 * 会员入口
 */

use app\api\model\UserInfo;
use app\common\controller\Api;
use app\common\library\Sms;
use function fast\e;
use fast\Random;
use think\Validate;

class User extends Api
{
    protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third'];
    protected $noNeedRight = '*';


    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 注册会员
     *
     * @param string $mobile   手机号
     * @param string $captcha   验证码
     */
    public function register(){
        if ($this->request->isPost()){
            $mobile = $this->request->request('mobile');
            $captcha = $this->request->request('captcha');
            if (!$mobile || !$captcha) {
                $this->error(__('Invalid parameters'));
            }
            if (!Validate::regex($mobile, "^1\d{10}$")) {
                $this->error(__('Mobile is incorrect'));
            }

            if (!Sms::check($mobile, $captcha, 'register')) {
                $this->error(__('Captcha is incorrect'));
            }

            $ret = $this->auth->register($mobile,  Random::alnum(), '', $mobile, []);
            if ($ret) {
                $data = ['userinfo' => $this->auth->getUserinfo()];
                $this->success(__('Sign up successful'), $data);
            } else {
                $this->error($this->auth->getError());
            }
        }
    }

    /**
     * 用户登录
     * @param string $account  账号
     * @param string $password 密码
     */
    public function login(){
        $mobile = $this->request->request('mobile');
        $password = $this->request->request('password');
        if (!$mobile || !$password) {
            $this->error(__('Invalid parameters'));
        }
        $ret = $this->auth->login($mobile, $password);
        if ($ret) {
            $data = ['userinfo' => $this->auth->getUserinfo()];
            $this->success(__('Logged in successful'), $data);
        } else {
            $this->error($this->auth->getError());
        }
    }

    /**
     * 手机验证码登录
     *
     * @param string $mobile  手机号
     * @param string $captcha 验证码
     */
    public function mobilelogin()
    {
        $mobile = $this->request->request('mobile');
        $captcha = $this->request->request('captcha');
        if (!$mobile || !$captcha) {
            $this->error(__('Invalid parameters'));
        }
        if (!Validate::regex($mobile, "^1\d{10}$")) {
            $this->error(__('Mobile is incorrect'));
        }
        if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
            $this->error(__('Captcha is incorrect'));
        }

        $user = \app\common\model\User::getByMobile($mobile);
        if ($user) {
            if ($user->status != 'normal') {
                $this->error(__('Account is locked'));
            }
            //如果已经有账号则直接登录
            $ret = $this->auth->direct($user->id);
        } else {
            $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
        }
        if ($ret) {
            Sms::flush($mobile, 'mobilelogin');
            $data = ['userinfo' => $this->auth->getUserinfo()];
            $this->success(__('Logged in successful'), $data);
        } else {
            $this->error($this->auth->getError());
        }
    }

    /**
     * 身份认证
     * @param string $token  Token
     * @param string $sfzimages 身份证照片地址
     * @param string $sfznumber 身份证号码
     * @param string $sfzstarttime 身份证有效期起始时间
     * @param string $sfzendtime 身份证有效期截止时间
     * @param string $jkzimage 健康证照片地址
     * @param string $jkzstarttime 健康证有效期起始时间
     * @param string $jkzendtime 健康证有效期截止时间
     * @param string $city 常用地址
     * @param string $address 详细地址
     *
     * /uploads/20200219/20fa9e3b46fb9b78d0c8c13a9c06af0f.jpg  // 正面身份证
     * /uploads/20200219/5a261cd478284b109d803415ed861fbf.jpg  // 反面身份证
     * /uploads/20200219/c24c10aa566f3c44230341acf68a5a1b.jpg  // 健康证
     */
    public function attestation(){
        if($this->request->isPost()){
            $data = $this->request->post();
            $userModel = new \app\api\model\User();
            $user = $userModel::get($this->auth->id,'userInfo')->toArray();
            if ($user['user_info']){
                if ($user['user_info']['status'] == 1){
                    $this->error(__('请等待系统认证'));
                }
                $this->error(__('认证信息已提交，请勿重复提交'));
            }
            if (!isset($data['sfzimages']) || $data['sfzimages'] == '') $this->error(__('身份证照片不能为空'));
            if (count(explode(',',$data['sfzimages'])) < 2) $this->error(__('身份证照片数量不足'));
            if (!isset($data['sfznumber']) || $data['sfznumber'] == '') $this->error('身份证号码不能为空');
            if ((!isset($data['sfzstarttime']) || $data['sfzstarttime'] == '')
                || (!isset($data['sfzendtime']) || $data['sfzendtime'] == '')) $this->error('身份证有效期不能为空');
            if (!isset($data['jkzimage']) || $data['jkzimage'] == '') $this->error(__('健康证照片不能为空'));
            if ((!isset($data['jkzstarttime']) || $data['jkzstarttime'] == '')
                || (!isset($data['jkzendtime']) || $data['jkzendtime'] == '')) $this->error('健康证有效期不能为空');
            if (!isset($data['city']) || $data['city'] == '') $this->error(__('常用地址不能为空'));
            if (!isset($data['address']) || $data['address'] == '') $this->error(__('详细地址不能为空'));

            $data['user_id'] = $this->auth->id;
            $data['createtime'] = time();
            $data['sfzstarttime'] = strtotime($data['sfzstarttime']);
            $data['sfzendtime'] = strtotime($data['sfzendtime']);
            $data['jkzstarttime'] = strtotime($data['jkzstarttime']);
            $data['jkzendtime'] = strtotime($data['jkzendtime']);
            unset($data['token']);
            if ($userModel->userInfo()->insert($data)){
                $this->success(__('提交成功'));
            }else{
                $this->error(__('提交失败'));
            }
        }
    }

}